import request from '../../utils/request';
// pages/index/index.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		bannerList: [], // 轮播图数据
		recommendList: [],
		topList: [],
	},
	handleGetUserInfo(res) {
		if (res.detail.userInfo) {
			this.setData({
				userInfo: res.detail.userInfo,
			});
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		let bannerListData = await request('/banner', { type: 2 });
		this.setData({
			bannerList: bannerListData.banners,
		});

		let recommendListData = await request('/personalized', { limit: 10 });
		this.setData({
			recommendList: recommendListData.result,
		});

		let index = 0;
		let resultArr = [];
		while (index < 5) {
			let result = await request('/top/list', { idx: index++ });
			// 因为返回的json对象太大，所以需要提前过滤
			let topListObj = {
				name: result.playlist.name,
				tracks: result.playlist.tracks.slice(0, 3),
			};
			resultArr.push(topListObj);
			this.setData({
				topList: resultArr,
			});
		}
	},
	handleParent() {
		console.log('parent');
	},
	handleChild() {
		console.log('child');
	},
	toLogs() {
		wx.redirectTo({
			url: '/pages/logs/logs',
		});
	},
	toRecommend() {
		wx.navigateTo({
			url: '/songPackage/pages/recommend/recommend',
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		console.log('初次渲染完成');
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		console.log('页面显示');
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {
		console.log('onHide');
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		console.log('onUnload');
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {},
});
